package proxyTest.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

public class JDkProxyTest implements InvocationHandler {


    private Object target;

    public JDkProxyTest(Object target) {
        super();
        this.target = target;
    }


    public Object createProxy(){
        ClassLoader classLoader = target.getClass().getClassLoader();
        Class<?>[] interfaces = target.getClass().getInterfaces();
        Object newProxyInstance = Proxy.newProxyInstance(classLoader, interfaces, this);
        return newProxyInstance;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("Before 增强 ...");
        Object invoke = method.invoke(target, args);
        System.out.println("After 增强 ...");
        return invoke;
    }

}
